#!/usr/bin/env python
# -*- coding:utf-8 -*-
from rest_framework import serializers
from apps.api import models

# 专场列表
class AuctionModelSerializer(serializers.ModelSerializer):
    status = serializers.SerializerMethodField()
    items = serializers.SerializerMethodField()
    cover = serializers.SerializerMethodField()
    # cover = serializers.CharField() # 专场封面图

    class Meta:
        model = models.Auction
        fields = ['id', 'title', 'status', 'cover', 'total_price', 'look_count', 'goods_count', 'items']

    def get_status(self, obj):
        status_class_mapping = {
            2: 'preview',
            3: 'auction',
            4: 'stop'
        }
        return {'text': obj.get_status_display(), 'class': status_class_mapping.get(obj.status)}
    # 拍品封面图
    def get_items(self, obj):
        queryset = models.AuctionItem.objects.filter(auction=obj)[0:5]
        content = []
        for row in queryset:
            cover = row.cover.name
            if not cover.startswith('http'):
                cover = "http://127.0.0.1:8000/media/" + cover
            content.append(cover)
        return content
    # 原始数据
    # def get_items(self, obj):
    #     queryset = models.AuctionItem.objects.filter(auction=obj)[0:5]
    #     return [row.cover.name for row in queryset]

    #*#临时添加，因为本地测试图片是传到本地，127.0.0.1
    def get_cover(self,obj):
        cover = str(obj.cover)
        if not cover.startswith('http'):
            cover = "http://127.0.0.1:8000/media/" + cover
        return cover

# 专场下的拍品
class AuctionDetailItemModelSerializer(serializers.ModelSerializer):
    # cover = serializers.CharField()
    cover = serializers.SerializerMethodField()
    status_class = serializers.SerializerMethodField()
    status_text = serializers.CharField(source='get_status_display')

    class Meta:
        model = models.AuctionItem
        fields = [
            'id', 'status', 'status_text', 'status_class', 'cover', 'unit', 'title', 'start_price',
            'deal_price', 'reserve_price', 'highest_price','look_count'
        ]

    def get_status_class(self, obj):
        """
        (2, '预展中'),
        (3, '拍卖中'),
        (4, '成交'),
        (5, '流拍'),
        (6, '逾期未支付'),
        """
        status_class_mapping = {
            2: 'preview',
            3: 'auction',
            4: 'deal',
            5: 'streaming',
            6: 'nodeposit'
        }
        return status_class_mapping.get(obj.status)

    #*#临时添加，因为本地测试图片是传到本地，127.0.0.1
    def get_cover(self,obj):
        cover = str(obj.cover)
        if not cover.startswith('http'):
            cover = "http://127.0.0.1:8000/media/" + cover
        return cover

# 专场详情
class AuctionDetailModelSerializer(serializers.ModelSerializer):
    status = serializers.SerializerMethodField()
    items = serializers.SerializerMethodField()
    deposit = serializers.SerializerMethodField(label='保证金')
    # cover = serializers.CharField()
    cover = serializers.SerializerMethodField()

    class Meta:
        model = models.Auction
        fields = ['id', 'status', 'title', 'cover', 'look_count', 'goods_count', 'bid_count', 'items', 'deposit']

    def get_status(self, obj):
        status_class_mapping = {
            2: 'preview',
            3: 'auction',
            4: 'stop'
        }
        return {'text': obj.get_status_display(), 'class': status_class_mapping.get(obj.status)}

    def get_items(self, obj):
        queryset = models.AuctionItem.objects.filter(auction=obj).exclude(status=1)
        ser = AuctionDetailItemModelSerializer(instance=queryset, many=True)
        return ser.data

    def get_deposit(self, obj):
        context = {
            'total': False,
            'single': {}
        }
        user_object = self.context['request'].user
        if not user_object:
            return context
        # 当前专场下的用户支付保证金成功
        queryset = models.DepositRecord.objects.filter(user=user_object, auction=obj, status=2)
        if not queryset.exists():
            return context
        # 是否缴纳的是全场保证金
        if queryset.filter(deposit_type=2).exists():
            context['total'] = True
            return context
        # 单品保证金{11: True}
        context['single'] = {row.item_id: True for row in queryset}
        return context
    #*#临时添加，因为本地测试图片是传到本地，127.0.0.1
    def get_cover(self,obj):
        cover = str(obj.cover)
        if not cover.startswith('http'):
            cover = "http://127.0.0.1:8000/media/" + cover
        return cover